package com.oymn.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.DefaultDatabaseErrorHandler;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.oymn.myapplication.page.register;
import com.oymn.myapplication.page.shouye;
import com.oymn.myapplication.utils.Url;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    protected static final int LOGIN_SUCCESS = 0;
    protected static final int LOGIN_FAIL = 1;

    Button login = null;
    Button register = null;
    Button forget_password = null;
    TextView username = null;
    TextView password = null;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case LOGIN_SUCCESS:
                    //如果登录成功的话，跳转到首页推荐页面
                    Intent intent = new Intent(MainActivity.this, shouye.class);
                    intent.putExtra("user_phone",username.getText().toString());
                    startActivity(intent);
                    break;
                case LOGIN_FAIL:
                    //登录失败
                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        login = findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取账户密码
                String userName = username.getText().toString();
                String passWord = password.getText().toString();

                //判断账户密码是否存在数据库中
                login(userName,passWord);
            }
        });

        register = findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, register.class);
                startActivity(intent);
            }
        });

        forget_password = findViewById(R.id.forget_password);
        forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"什么蠢比还会忘记密码",Toast.LENGTH_SHORT).show();
            }
        });
    }

    //登录验证
    public void login(String username, String password){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(Url.user_url);

                //封装参数
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("action","login"));
                param.add(new BasicNameValuePair("user_phone",username));
                param.add(new BasicNameValuePair("user_password",password));

                try {
                    request.setEntity(new UrlEncodedFormEntity(param));
                    //获取响应的结果
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity entity = response.getEntity();
                    //获取响应结果的信息
                    String result = EntityUtils.toString(entity,"UTF-8");
                    if("login success".equals(result)){
                        Message msg = new Message();
                        msg.what = LOGIN_SUCCESS;
                        handler.sendMessage(msg);
                    }
                    else if("phone or password not exist".equals(result)){
                        Message msg = new Message();
                        msg.what = LOGIN_FAIL;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}